<?php 
class ChartsController extends AppController {

	var $name = 'Charts';
	
	var $uses = array();
	var $helpers = array('Html', 'Form');
    
    function calculateDistance($lat_a,$lat_b,$long_a,$long_b) {
        if($lat_a==$lat_b&&$long_a==$long_b)
            return 0;
            
        $lat_a = $lat_a * PI()/180;
        $lat_b = $lat_b * PI()/180;
        $long_a = $long_a * PI()/180;
        $long_b = $long_b * PI()/180;
        $distance =
                acos(
                        sin($lat_a ) * sin($lat_b) +
                        cos($lat_a) * cos($lat_b) * cos($long_b - $long_a)
                ) * 6371;
       
        return $distance;
    }
	
    
 /**
 * Ham p_test($user_id,$location_id,$hots) 
 * ==>tham khao tu PREDICT o newcheckins, CHAY TREN DU LIEU TRAINING, SO SANH VOI TEST
 * xac dinh ro anh huong di chuyen tu dau dua vao 3 yeu to:
 * +di chuyen ca nhan (1)
 * +di chuyen den nhug hot regions (2)
 * +di chuyen do anh huong ban
 * 
 * @param user_id       User se xet
 * @param location_id   Location de du doan
 * @param hots          Vung hot regions danh cho all users
 * 
 * @return  1,0         1: du doan user den, 0: du doan user khong den dia diem da cho
 * 
 * Ham duoc xay dung tren co so model voi xac suat dung 
 * max=91% for 100 rows (96% yes, 86% no), 
 * 88% for testing data set
 * */
    
 /*
  * NHUNG CONTROLLER SU DUNG:
  
  * NHUNG ANH XA DU LIEU TRAINING VA TEST
  * 
  * Newcheckin                  Checkinstrain     
  * Friendship                      
  * FulltrainTonghopWithoutF    TrainTonghopWithoutF
  * FulltrainsULocationTable    TrainsULocationTable
  * LocationNumcksTb
  * 
 */
    
    
    function p_test($user_id,$location_id,$hots){

    
        $this->Newcheckin->recursive=-1;
        //tim lich su di chuyen cua user    
        $his=$this->Newcheckin->find('list',
                array(
                    'conditions'=>array('user_id'=>$user_id)
                    ,'fields'=>array('location_id','location_id')
                    //,                    'order'=>array('c'=>'desc')
                    //,                    'limit'=>2
                )
        );
/** if so 1, xet lich su di chuyen cua user */        
        if(in_array($location_id,$his))
            return 1;
/** ELSE so 1 */
        else
        {
/** if so 2, xet xem dia phuong dang xet co nam trong hot regions */
            if(in_array($location_id,$hots))
                return 1;
/** else so 2 */
            else          
            {
       
                $this->Friendship->recursive=-1;
                $fs=$this->Friendship->find('list',
                 array('fields'=>array('withuser','withuser'),
                       'conditions'=>array('user_id'=>$user_id)));
       
                $this->Newcheckin->recursive=-1;
                $f_predicted=$this->Newcheckin->find('list',
                        array(
                        'conditions'=>array('user_id'=>$fs)
                    ,                    'fields'=>array('location_id','location_id')
                ));
/** if so 3, xet history cua ban */
                if(in_array($location_id,$f_predicted))
                    return 1;
/** ELSE so 3, bat dau xet den khoang cach "bounded regions" */
                else{
                       
//tim dia phuong user hay toi nhat
                        $this->FulltrainTonghopWithoutF->recursive=-1;
                        $f_dis_user=$this->FulltrainTonghopWithoutF->find('first',
                            array(
                            'conditions'=>array(
                            'user_id'=>$user_id))
                        );

                        $this->FulltrainsULocationTable->recursive=-1;
                        $t_2latents=$this->FulltrainsULocationTable->find('all',
                            array(
                                'conditions'=>array('user_id'=>$user_id)
                        //         ,'fields'=>array('location_id','location_id')
                             ,            'order'=>array('num_checks'=>'desc')
                                ,           'limit'=>1
                        ));
                        $this->LocationNumcksTb->recursive=-1;
                        $ls_considered=$this->LocationNumcksTb->find('first',array(
                            'conditions'=>array(
                                'num_cks >= ? '=>array(5),
                                'location_id'=>$location_id),
                            'fields'=>array('x','y'))
                        );
                        $k1=count($t_2latents);
/** FOR 2, xet nhug dia diem thuong toi cua user va bounded region*/
                        foreach($t_2latents as $b)
                        {
       
                            $kc1=abs($this->calculateDistance(
                                 $b['FulltrainsULocationTable']['x'],
                                $ls_considered['LocationNumcksTb']['x'],
                                $b['FulltrainsULocationTable']['y'],
                                $ls_considered['LocationNumcksTb']['y']
                             ));
                            $max=0;
                            if($f_dis_user['FulltrainTonghopWithoutF']['max_move']<=10)
                                $max=10;
                            else
                                $max=$f_dis_user['FulltrainTonghopWithoutF']['max_move'];
                          
/** IF 4, du doan nam trong bounded region cua user */
                            if($max>=$kc1)
                                return 1;
                        } /** HET FOR 2*/

                        $this->FulltrainTonghopWithoutF->recursive=-1;
                        $f_dis=$this->FulltrainTonghopWithoutF->find('all',
                                array('conditions'=>array('user_id'=>$fs)));
                        $chay=count($f_dis);
/**  FOR 3, chay qua tung BAN **/
                        for($k=0;$k<$chay;$k++)
                        {
                                $khoang_cach=abs($this->calculateDistance(
                                    $f_dis[$k]['FulltrainTonghopWithoutF']['x'],
                                    $ls_considered['LocationNumcksTb']['x'],
                                    $f_dis[$k]['FulltrainTonghopWithoutF']['y'],
                                    $ls_considered['LocationNumcksTb']['y']
                                ));
                                   $max=0;
                  //      if($chan==0){
                                if($f_dis_user['FulltrainTonghopWithoutF']['max_move']<=10)
                                    $max=10;
                                else
                                    $max=$f_dis_user['FulltrainTonghopWithoutF']['max_move'];
                      
/** IF 5, du doan cac hot regions trong bounded region of friends * */
                                if($max>=$khoang_cach)
                                    return 1;                                    
                                   
                        }/** HET FOR 3**/
                }/** HET IF 3**/
                            
                         
                 
            }/** HET ELSE 2**/
                   
            
          
        }/** HET ELSE 1**/
//neu khong nam trong cac vung da cho, nghia la du doan user se khong den
    return 0;
    }/** HET FUNCTION**/

}
?>